package com.tanhua.server.controller;

import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.server.service.SoundService;
import com.tanhua.server.vo.SoundVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/*桃花传音的controller*/
@RestController
@RequestMapping("peachblossom")
@Slf4j
public class SoundController {
    @Autowired
    private SoundService soundService;

    /**
     * 发布语音
     *
     * @param soundFile
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveSound(@RequestParam("soundFile") MultipartFile soundFile) {
        try {
            Boolean bool = this.soundService.saveSound(soundFile);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 接收语音
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<SoundVo> receiveSound() {
        try {
            SoundVo soundVo = this.soundService.receiveSound();
            if (null != soundVo) {
                return ResponseEntity.ok(soundVo);//放在响应体中响应回去
            }
        } catch (Exception e) {
            log.error("接收语音消息出错~ userId = " + UserThreadLocal.get().getId(), e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    //对重要的功能我们进行try-catch处理
}
